//
//  SSUnserviceOrderListController.swift
//  拾舍清洁
//
//  Created by sky.wei on 2023/6/27.
//

import UIKit

class SSUnserviceOrderListController: UIViewController, UICollectionViewDelegate,UICollectionViewDataSource {
    
    @IBOutlet weak var tableView: UITableView!
    
    @IBOutlet weak var collectionView: UICollectionView!
    
    var items = [SSEmployeeOrder]()
    
    
    var dates = [SSEmployeeOrder]()
    var currentDate = SSEmployeeOrder()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        title = "待服务"
        
        initViews()
        getDateNetwork()
    }

    func initViews() {
        
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.register(UINib(nibName: "SSUnserviceLineCell", bundle: Bundle.init(for: SSUnserviceLineCell.self)), forCellWithReuseIdentifier: "SSUnserviceLineCell")
        
        // 竖屏时每行显示4张图片
        let shape: CGFloat = 5
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.scrollDirection = .horizontal
        flowLayout.itemSize = CGSizeMake(72, 84)
        flowLayout.minimumLineSpacing = shape
        flowLayout.minimumInteritemSpacing = shape
        collectionView.setCollectionViewLayout(flowLayout, animated: true)

        tableView.register(UINib(nibName: "SSNewOrderCell", bundle: Bundle.init(for: SSNewOrderCell.self)), forCellReuseIdentifier: "SSNewOrderCell")
//        tableView.backgroundColor = UIColor(hex: "DFEFFE")
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        self.tableView.es.addPullToRefresh {
            [unowned self] in
            self.tableView.es.stopPullToRefresh()
            getDateNetwork()
            print("1233")
        }
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 10, bottom:10, right: 10)
//        return UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
                let cell =
        collectionView.dequeueReusableCell(withReuseIdentifier: "SSUnserviceLineCell", for: indexPath) as! SSUnserviceLineCell
        cell.model = dates[indexPath.row]
        
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dates.count
    }
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }


    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
        for item in self.dates{
            item.isSelected = false
        }
        let m = self.dates[indexPath.row]
        m.isSelected = true
        self.currentDate = m
        self.getBannerNetwork(date: m.date )
    }

}

extension SSUnserviceOrderListController: UITableViewDelegate, UITableViewDataSource
{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc = SSOrderDetailController()
        vc.orderNo = self.items[indexPath.row].orderNo
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 190
    }
  
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SSNewOrderCell") as! SSNewOrderCell
        cell.resultClosure {
            self.getBannerNetwork(date: self.currentDate.date)
            self.currentDate.taskCount = self.currentDate.taskCount - 1
            self.collectionView.reloadData()
        }
        cell.model = items[indexPath.row]
        return cell
    }
    
   
}


extension SSUnserviceOrderListController {
    
    //banner
    func getDateNetwork() {
        let param = [
            "phoneNo": kUserPhone
        ] as [String:AnyObject]
//        SSProgressHUD.share.show()
        HttpConnectTools.share.request(urlString: APIUrl.share.getEMPCleanTask_URL, parameters: param) { (result, error) in
//            SSProgressHUD.share.dismiss()

            if let _ = result{

                guard let banners = [SSEmployeeOrder].deserialize(from: result, designatedPath:"data.listTask") else {
                    return
                }
                self.dates = banners as! [SSEmployeeOrder]
                self.collectionView.reloadData()
                
                if(self.currentDate.date.count == 0){
                    let m = self.dates.first
                    m?.isSelected = true
                    self.currentDate = m!
                }
                self.getBannerNetwork(date: self.currentDate.date )
            }else{
            }
        }
        
       
    }
    
    //banner
    func getBannerNetwork(date:String) {
        let param = [
            "pageCurrent": "1",
            "pageSize": "100",
            "phoneNo": kUserPhone,
            "type": "1",
            "queryDate":date
            
        ] as [String:AnyObject]
        
        
        SSProgressHUD.share.show()
        HttpConnectTools.share.request(urlString: APIUrl.share.getEmployeeOrder_URL, parameters: param) { (result, error) in
            if let _ = result{
                SSProgressHUD.share.dismiss()
                
                guard let banners = [SSEmployeeOrder].deserialize(from: result, designatedPath:"data.listOrder") else {
                    return
                }
                self.items = banners as! [SSEmployeeOrder]
                self.tableView.reloadData()
                self.collectionView.reloadData()
            }else{
            }
        }
    }
    
}

